#include "head/applicationdata.h"
#include "head/ircclientstarter.h"
#include "head/ircclient.h"
#include "head/httpserver.h"
#include "head/global.h"

#include <QThread>
#include <QApplication>
#include <QDebug>

int main(int argc, char *argv[])
{
    if (argc < 2) {
        qInfo().noquote() << "No parameters passed. Use -h or --help for usage information.";
        return 1;
    }

    // To start QApplication without X-server (non-GUI system) should use:
    // "export QT_QPA_PLATFORM=offscreen" in plain shell
    // or
    // "Environment=QT_QPA_PLATFORM=offscreen" in systemd service ([Service] section)

    QApplication a(argc, argv, false);

    QString configFile;
    for (int i = 1; i < argc; i++) {
        QString param(argv[i]);
        if ((param == "--example" or param == "-e") and i+1 < argc) {
            ApplicationData::createConfigExample (QString{argv[i+1]});
            return 0;
        }
        else if (param == "--help" or param == "-h") {
            qInfo().noquote() << "Usage: Pass the configuration file via --config and let's go!\n"
                                 "Possible arguments:\n"
                                 "-e --example <output file> - create example config file\n"
                                 "-c --config <input file>   - path to configuration file\n"
                                 "-v --version               - print version";
            return 0;
        }
        else if ((param == "--config" or param == "-c") and i+1 < argc) {
            configFile = QString{argv[i+1]};
        }
        else if ((param == "--version" or param == "-v")) {
            qInfo().noquote() << "IRCaBot" << global::IRCABOT_VERSION;
            return 0;
        }
    }
    if (configFile.isEmpty()) {
        qInfo().noquote() << "Configuration file not passed. Use -h or --help for usage information.";
        return 1;
    }

    qInfo().noquote() <<"\
 /$$$$$$  /$$$$$$$    /$$$$$$             /$$$$$$$               /$$    \n\
|_  $$_/ | $$__  $$  /$$__  $$           | $$__  $$             | $$    \n\
  | $$   | $$  \\ $$ | $$  \\__ /  /$$$$$  | $$  \\  $$  /$$$$$$  /$$$$$$  \n\
  | $$   | $$$$$$$/ | $$        |____ $$ | $$$$$$$   /$$__  $$|_  $$_/  \n\
  | $$   | $$__  $$ | $$         /$$$$$$ | $$__  $$ | $$  \\ $$  | $$    \n\
  | $$   | $$  \\ $$ | $$    $$  /$$__ $$ | $$  \\ $$ | $$  | $$  | $$ /$$\n\
 /$$$$$$ | $$  | $$ |  $$$$$$/|   $$$$$$ | $$$$$$$/ |  $$$$$$/  |  $$$$/\n\
|______/ |__/  |__/  \\______/   \\______/ |_______/   \\______/    \\___/";
    qInfo().noquote() << "IRCaBot" << global::IRCABOT_VERSION << "| Source code: https://notabug.org/acetone/ircabot";
    qInfo().noquote() << "GPLv3 (c) acetone," << global::COPYRIGHT_YEAR << "\n";

    ApplicationData configuration(configFile);
    auto webInterfaceAddress = configuration.getWebInterfaceAddress();
    HttpServer webInterface(webInterfaceAddress.first,
                            webInterfaceAddress.second,
                            configuration.getDataFolder(),
                            configuration.getServiceName(),
                            configuration.getServiceEmoji(),
                            configuration.getAjaxIsDisabled());

    auto serversData = configuration.getConnections();
    for (auto server: serversData) {
        if (not server.isOk()) continue;
        IrcClientStarter* wrapper = new IrcClientStarter(server, &webInterface);
        QThread* thread = new QThread;
        wrapper->moveToThread(thread);
        QObject::connect (thread, SIGNAL(started()), wrapper, SLOT(RunInit()));
        thread->start();
    }

    return a.exec();
}
